<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>评估</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="robots" content="all,follow">
    <!-- Bootstrap CSS-->
    <link rel="stylesheet" href="vendor/bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome CSS-->
    <link rel="stylesheet" href="vendor/font-awesome/css/font-awesome.min.css">
    <!-- Fontastic Custom icon font-->
    <link rel="stylesheet" href="css/fontastic.css">
    <!-- theme stylesheet-->
    <link rel="stylesheet" href="css/style.default.css" id="theme-stylesheet">
    <!-- Custom stylesheet - for your changes-->
    <link rel="stylesheet" href="css/custom.css">
    <!-- Favicon-->
    <link rel="shortcut icon" href="img/favicon.ico">
    <!-- Tweaks for older IEs--><!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script><![endif]-->
  </head>
  <body>
  	 <p id="shdow" style="z-index: 99;background-color: gray;display: block;position: absolute;opacity: 0.3;display: none;">
  	 	<img id="p_img" style="z-index: 100;background-color: gray;display: block;position: absolute;opacity: 0.3;"  src="img/loadding.png" />
  	 </p>
    <div class="page">
     
        
      <div class="page-content d-flex align-items-stretch"> 
        <!-- Side Navbar -->
       
        <div class="content-inner" style="width: 100%;">
          <!-- Page Header-->
         
          <!-- Forms Section-->
          <section class="forms no-padding-top"> 
            <div class="container-fluid">
              <div class="row">
              
                <!-- Form Elements -->
                <div class="col-lg-12" >
                  <div class="card" >
                    <div class="card-close">
                      <div class="dropdown">
                        <button type="button" id="closeCard5" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="dropdown-toggle"><i class="fa fa-ellipsis-v"></i></button>
                        <div aria-labelledby="closeCard5" class="dropdown-menu dropdown-menu-right has-shadow"><a href="#" class="dropdown-item remove"> <i class="fa fa-times"></i>Close</a><a href="#" class="dropdown-item edit"> <i class="fa fa-gear"></i>Edit</a></div>
                      </div>
                    </div>
                    <div class="card-header d-flex align-items-center">
                      <h3 class="h4" id="objName"></h3>
                    </div>
                    <div class="card-body">
                      <form id="obj_attr_area" class="form-horizontal">
                      	
                      	
                      	
                      

                       
                        
                        
                        
                        
                      </form>
                      <form id="report_area" class="form-horizontal">
                      	
                      	
                      	
                      

                       
                        
                        
                        
                        
                      </form>
                      
                      <div class="line"></div>
                        <div class="form-group row">
                          <div id="controlBtnArea" class="col-sm-4 offset-sm-3">
                            
                            <label id="saveBtn"  class="btn btn-primary">保存</label>
                            <label id="submitBtn"  class="btn btn-primary" style="display:none;">提交审核</label>
                            <label id="nextBtn"  class="btn btn-primary" style="display:none;">提交并选点</label>
                          </div>
                        </div>
                        
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </section>
         
        </div>
      </div>
    </div>
    <!-- JavaScript files-->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/popper.js/umd/popper.min.js"> </script>
    <script src="vendor/bootstrap/js/bootstrap.min.js"></script>
    <script src="vendor/jquery.cookie/jquery.cookie.js"> </script>
    <script src="vendor/chart.js/Chart.min.js"></script>
    <script src="vendor/jquery-validation/jquery.validate.min.js"></script>
    <!-- Main File-->
    <script src="js/front.js"></script>
    <script type="text/javascript" src="js/getUrlParam.js" ></script>
    <script type="text/javascript">
    	var objId = UrlParm.parm("No");
    	var report ="";
    	init();
    	$("#saveBtn").bind('click',function(){save()});
    	$("#submitBtn").bind('click',function(){submit()});
    	$("#nextBtn").bind('click',function(){selectPoint()});
    	
    	
    	function init(){
    		//显示遮罩
    			showCover();
    			//获取所有属性
    			 $.ajax({
              type:"get",
              url:"/business.spr?method=getAttrs",
              contentType:"application/json",
              dataType: "json",
              data:{
                  'param':getConfigCode(objId)
                  
             },
              success:function(data){
                  var dataObj=eval("("+data+")");

                  if(dataObj.result!="0000"){
                      alert(dataObj.msg);
                  }else{
                  	//清空表格区域
                  	$("#obj_attr_area").empty();
                  	var attrs = dataObj.params.attrs;
                  	var temp ="";
                  	for(var i=0; i<attrs.length; i++){
                  		temp = temp+appendElement(attrs[i].parm_code,attrs[i].parm_value,attrs[i].descch,0);
                  	}
                  	//展示属性
                  	$("#obj_attr_area").append(temp);
                  	
                  	 //属性加载完成后 填充select属性
        							fullSelect();
		              
                  }
              },
              error:function(){
              	
              	 
                  alert("属性获取失败！");

              }
          });
         
          
				
}
    	
    	function getReportConfig(){
    		
    		 $.ajax({
              type:"get",
              url:"/business.spr?method=getAttrs",
              contentType:"application/json",
              dataType: "json",
              data:{
                  'param':"report"
                  
             },
              success:function(data){
                  var dataObj=eval("("+data+")");

                  if(dataObj.result!="0000"){
                      alert(dataObj.msg);
                  }else{
                  	
                  	var attrs = dataObj.params.attrs;
                  	var temp ="";
                  	for(var i=0; i<attrs.length; i++){
                  		temp = temp+appendElement(attrs[i].parm_code,attrs[i].parm_value,attrs[i].descch,1);
                  	}
                  	//展示属性
                  	report = temp;
                  	//加载报告参数框
                  	showReportAttr();	
		              
                  }
              },
              error:function(){
              	
              	 
                  alert("属性获取失败！");

              }
          });
         
    		
    	}
    	
    	//处理类型  传入id 解析后返回参数代码
    	function getConfigCode(No){
    		var temp = No.substr(-5,1);
    		var code ='';
    		switch(temp){
    			case 'L':
	    			code = "attr_land";
	    			break;
	    		case 'B':
	    			code = 'attr_building';
	    			break;
	    		default:
	    			code = "其他";
	    			break
    		}
    		return code;
    	}
  
    	
    	function appendElement(parm_code,parm_value,descch,report){
    		var classflag = "attr_value";
    			var temp = "";
    		if(report==1){
    			classflag = "report";
    		}
    		
    	
    		
    		switch(parm_value){
    			case "input":
    			{
    				temp = ' <div class="form-group row no-padding-bottom">'+
                          '<label class="col-sm-3 form-control-label">'+descch+'</label>'+
                          '<div class="col-sm-9">'+
                            '<input type="text"  id="'+parm_code+'" class="form-control '+classflag+'"'+'>'+
                          '</div> </div>';
    				
    			}
    			break;
    			case "select":
    			{
    				temp = ' <div class="line"></div>'+
                        '<div class="form-group row no-padding-bottom">'+
                          '<label class="col-sm-3 form-control-label">'+descch+'</label>'+
                          '<div class="col-sm-9">'+
                            '<select  id="'+parm_code+'" class="form-control mb-3 '+classflag+'"'+'>'+
                             
                            '</select>'+
                          '</div> </div><div class="line"></div>';
    			}
    			break;
    			case "text":
    			{
    				temp = '<div class="line"></div>'+
                        '<div class="form-group row" style="height: auto;">'+
                          '<label class="col-sm-3 form-control-label">'+descch+'</label>'+
                          '<div class="col-sm-9" style="height: auto;">'+
                           
                            '<textarea class="form-control '+classflag+'"'+' rows="5" id="'+parm_code+'" ></textarea>'+
                            '<small class="help-block-none">可拖拽右下角调整输入框大小.</small>'+
                          '</div></div><div class="line"></div>';
    				
    			}
    			break;
    			case "option":
    			{
    				temp = '<option value="'+parm_code+'">'+descch+'</option>';
    			}
    			
    		}
    		return temp;
    		
    		
    	}
    	
    	
    	function fullSelect(){
    		
    		var selects = $("#obj_attr_area").find("select");
    		//若不需加载下拉框 则开始填充数据
    		if(selects.length == 0){
    				fullValue();
    		}
    		
    		  selects.each(function(i,e){
          	var id = $(e).attr("id");
          	 $.ajax({
              type:"get",
              url:"/business.spr?method=getAttrs",
              contentType:"application/json",
              dataType: "json",
              data:{
                  'param':id
                  
             },
              success:function(data){
                  var dataObj=eval("("+data+")");

                  if(dataObj.result!="0000"){
                      alert(dataObj.msg);
                  }else{
                  	
                  	
                  	var attrs = dataObj.params.attrs;
                  	var temp ="";
                  	for(var i=0; i<attrs.length; i++){
                  		temp = temp+appendElement(attrs[i].parm_code,attrs[i].parm_value,attrs[i].descch,0);
                  	}
                  	//展示属性
                  	$("#"+id).append(temp);
                  	
		              fullValue();
                  }
              },
              error:function(){
              	
              	 
                  alert("下拉框属性获取失败！");
									fullValue();
              }
          });
          	
          	
          	
          });
          
          //下拉框加载完成后  显示 下方按钮
          if($("#issecondhand")!=undefined){
          	$("#issecondhand").bind('change',function(){
          		
          		showBtn($(this).val());
          		
          		
          	});
          }
          
          
    	}
    	
    	function showBtn(isc){
    		//是二手房 清空报告区域
          		if(isc==1){
          			$("#report_area").empty();
          			$("#submitBtn").css('display',"none");
          			$("#nextBtn").css('display',"inline-block");
          			
          		}
          		else{
          			//非二手房 加载报告配置
          			notSecondHandBuildingJudge();
          			$("#submitBtn").css('display',"inline-block");
          			$("#nextBtn").css('display',"none");
          		}
          		
    	}
    	
    	//获取数据  填充数据
    	function fullValue(){
    		hideCover();
    		 $.ajax({
              type:"get",
              url:"/business.spr?method=getObjInf",
              contentType:"application/json",
              dataType: "json",
              data:{
                  'objId':objId
                  
             },
              success:function(data){
                  var dataObj=eval("("+data+")");

                  if(dataObj.result!="0000"){
                      alert(dataObj.msg);
                  }else{
                  
                  	
                  	var attrs = dataObj.params.attrs;
                  	for(var key in attrs){
                  		if(attrs[key]!=null){
                  			$("#"+key).val(attrs[key]);
                  		}
                 	
                  	}
                  	
                  	//当所有数据加载完成后  判断是否为二手房
                  	
                  	if($("#issecondhand").val()==0){
                  		notSecondHandBuildingJudge();
                  	}
                  	showBtn($("#issecondhand").val());
                  	
                  }
              },
              error:function(){
              	
              	 
                  alert("属性获取失败！");

              }
          });
    		
    	}
    	
    	//获取数据  填充数据
    	function getReportData(){
    		hideCover();
    		 $.ajax({
              type:"get",
              url:"/business.spr?method=getObjInf",
              contentType:"application/json",
              dataType: "json",
              data:{
                  'objId':objId
                  
             },
              success:function(data){
                  var dataObj=eval("("+data+")");

                  if(dataObj.result!="0000"){
                      alert(dataObj.msg);
                  }else{
                  
                  	
                  	var attrs = dataObj.params.attrs;
                  	for(var key in attrs){
                  		if(attrs[key]!=null){
                  			$("#"+key).val(attrs[key]);
                  		}
           
                  	}
                  	
                  	
                  }
                  hideCover();
              },
              error:function(){
              	hideCover();
              	 
                  alert("属性获取失败！");

              }
          });
    		
    	}
    	
    	function notSecondHandBuildingJudge(){
    		
    		
    		if(report==""){
    			getReportConfig();
    		}
    		else{
    		showReportAttr();	
    		}
    		
    		getReportData();
    		
    		
    	}
    	
    	function showReportAttr(){
    		$("#report_area").empty();
    		$("#report_area").append(report);
    	}
    	
    	function showCover(){
    		$("#shdow").css("height",document.body.scrollHeight*2);
    		$("#shdow").css("width",document.body.scrollWidth *2);
    		$("#p_img").css("height",document.body.scrollHeight);
    		$("#p_img").css("width",document.body.scrollWidth );
    		$("#shdow").show();

    	}
    	function hideCover(){
    		$("#shdow").css("display","none");
    	}
    	
    	function save(){

    		var attrvalues = new Object();
    		var reportvalues = new Object();
    		attrvalues["objId"]=objId;
    		reportvalues["objId"]=objId;
    		
    		$(".attr_value").each(function(i,e){
    			if($(e).val()==""||$(e).val()==undefined){
    				//属性为空则不传
    			}else{
    				attrvalues[$(e).attr("id")]=$(e).val();
    			}
    			
    		});
    		//提交时  传入下一步code 评估中
    			attrvalues['step'] = 2;
    		
    		$.ajax({
    			type:"post",
    			url:"business.spr?method=updateObj",
    			contentType:"application/json",
              dataType: "json",
              data:JSON.stringify(attrvalues),
              success:function(data){
              	dataObj = eval("("+data+")");
              	
              	if(dataObj.result!="0000"){
                      alert(dataObj.msg);
                  }else{
                  	
                  	alert("保存成功");
                  }
                },
                error:function(){
                	 alert("保存失败！");
                }
              });
              //若非二手房  需要保存报告数据
              if($(".report").length>0){
              	
              	var upload = 0;
              		$(".report").each(function(i,e){
              			if($(e).val()==""||$(e).val()==undefined){ 
              				//属性为空则不传 
              			}else{
              				reportvalues[$(e).attr("id")]=$(e).val();
              				upload++;
              			}
              			
              			});
              			
              			if(upload>0){
              				$.ajax({
								    			type:"post",
								    			url:"business.spr?method=updateReport",
								    			contentType:"application/json",
								              dataType: "json",
								              data:JSON.stringify(reportvalues),
								              success:function(data){
								              	dataObj = eval("("+data+")");
								              	
								              	if(dataObj.result!="0000"){
								                      alert(dataObj.msg);
								                  }else{
								                  	
								                  	
								                  }
								                },
								                error:function(){
								                	 alert("保存报告信息失败！");
								                }
								              });
              	
              			}
              	
              	
              	
              }
    		
    		
    		
    		
    		
    		
    	}
    	
    	var nextstep = 1;
    	
    	function submit(){
    		var reportvalues = new Object();
    		var attrvalues = new Object();
    		
    		attrvalues["objId"]=objId;
    		reportvalues["objId"]=objId;
    		var flag = 1;
    		
    		$(".attr_value").each(function(i,e){
    			if($(e).val()==""||$(e).val()==undefined){
    				flag = 0;
    				alert("该输入项不能为空！");
    				$(e).focus();
    				attrvalues = new Object();
    				return false;
    			}
    			attrvalues[$(e).attr("id")]=$(e).val();
    		});
    		//提交时  传入下一步code 评估完成待审核
    			attrvalues['step'] = 3;
    		
    		
    		$(".report").each(function(i,e){
    			if($(e).val()==""||$(e).val()==undefined){
    				flag = 0;
    				alert("该输入项不能为空！");
    				$(e).focus();
    				reportvalues = new Object();
    				return false;
    			}
    			reportvalues[$(e).attr("id")]=$(e).val();
    		});
    		
    		
    		if(flag==0){
    			return;
    		}
    		
    		//验证通过  提交数据
    		//console.log(attrvalues);
    		//console.log(JSON.stringify(attrvalues));
    		
    		var submitflag = 1;
    		
    		$.ajax({
    			type:"post",
    			url:"business.spr?method=updateObj",
    			contentType:"application/json",
              dataType: "json",
              data:JSON.stringify(attrvalues),
              success:function(data){
              	dataObj = eval("("+data+")");
              	
              	if(dataObj.result!="0000"){
                      alert(dataObj.msg);
                  }else{
                  	//保存成功
                  	
                  	
                  }
                },
                error:function(){
                	nextstep = 0;
                	 submitflag = 0;
                	 alert("保存失败！");
                }
              });
    		
    		$.ajax({
    			type:"post",
    			url:"business.spr?method=updateReport",
    			contentType:"application/json",
              dataType: "json",
              data:JSON.stringify(reportvalues),
              success:function(data){
              	dataObj = eval("("+data+")");
              	
              	if(dataObj.result!="0000"){
                      alert(dataObj.msg);
                  }else{
                  	//保存成功
                  	
                  	
                  }
                },
                error:function(){
                	nextstep = 0;
                	 submitflag = 0;
                	 alert("保存失败！");
                }
              });
    		//若非二手房  需要保存报告数据
              if($(".report").length>0){
              	
              
              				$.ajax({
								    			type:"post",
								    			url:"business.spr?method=updateReport",
								    			contentType:"application/json",
								              dataType: "json",
								              data:JSON.stringify(reportvalues),
								              success:function(data){
								              	dataObj = eval("("+data+")");
								              	
								              	if(dataObj.result!="0000"){
								                      alert(dataObj.msg);
								                  }else{
								                  	
								                  	
								                  }
								                },
								                error:function(){
								                	nextstep = 0;
								                	 alert("保存报告信息失败！");
								                }
								              });
              	
              			
              	
              	
              	
              }
    		
    		
    		
    		
    		
    	}
    	
    	function selectPoint(){
    		if(submit()!=0)
    		parent.addTabs("地图选点","/map.html?address="+$("#position").val(),1,null);
    		
    	}
    	
    </script>
  </body>
</html>
